<?
/**
 * system.php
 * 
 * <pre>
 * Devyra Internacionalnaya
 * Vebba PHP Framework and Website Builder
 * Main source code file of framework
 * </pre>
 *
 * @package   classes.core.static
 * @version   0.7.0100 alpha
 * @since     2012
 * @author    Paul Dinh
 * @copyright Devyra Internacionalnaya
 */
 
/**
 * Main source class of framework
 * 
 * @package   classes.core.static
 * @version   0.7.0100 alpha
 * @since     2012
 * @author    Paul Dinh
 * @copyright Devyra Internacionalnaya
 */ 
class system {

  /* 
   * All properties
   */
  public static $Configs = array();
  public static $Classes = array(); //static core classes only
  public static $Miscs   = array();

  /**
   * Initialise the whole framwork
   */
  public static function initialise() {
    ob_start();
    self::load_core_classes();
    
    //some preparations
    self::$Configs = xml::arr_from_file("config.xml");
    request::parse_query_str();
    self::check_preferences();
    
    //call to initialisations in static core classes;
    //note that class 'system' is not in the list
    //so it won't call itself;
    foreach (self::$Classes as $Class_Name)
      eval($Class_Name."::initialise();");
      
    //base classes like controller, model, etc.
    self::load_base_classes();
    self::load_skin_files();
  }

  /**
   * Check user preferences like language, skin, etc.
   */
  private static function check_preferences() {
    $Module = request::$Module;
    $Params = request::$Params["index"];
    
    //check if language abbreviation indicated in url
    if (val($Params,"lang")!=null) {
      $Lang = $Params["lang"];      
      cookie::set($Module."_lang",$Lang);
    }
    
    //get current lang
    $Lang = cookie::get($Module."_lang"); 
    if ($Lang!=null)
      phrase::$Lang = $Lang;
  }
  
  /**
   * Load all core static classes (except 'system')
   */
  private static function load_core_classes() {
    $Dir_Path = "classes/core/static";
    $Items = scandir($Dir_Path);    
    self::$Classes = array();
    
    //load all core static classes
    foreach ($Items as $Item) {
      $File_Path = $Dir_Path."/".$Item;
      $Parts     = pathinfo($File_Path);
      $File_Name = strtolower($Parts["filename"]);
      $Extension = strtolower($Parts["extension"]);
      
      //check if is code file
      if (is_file($File_Path) && $Extension=="php") {
        require_once($File_Path);
        $Class_Name = $File_Name;
        if ($Class_Name!="system")
          array_push(self::$Classes,$Class_Name);
      }
    }//foreach
  }
  
  /**
   * Load all base classes
   */
  private static function load_base_classes() {
    $Dir_Path = "classes/core/base";
    $Load = xml::arr_from_file($Dir_Path."/load.xml");
    $Items = $Load["class"];
    
    //load all core base classes
    foreach ($Items as $Item) {
      $File_Path = $Dir_Path."/".$Item;
      $Parts     = pathinfo($File_Path);
      $File_Name = strtolower($Parts["filename"]);
      $Extension = strtolower($Parts["extension"]);
      
      //check if is code file
      if (is_file($File_Path) && $Extension=="php") 
        require_once($File_Path);
    }//foreach
  }
  
  /**
   * Load skin files
   */
  private static function load_skin_files() {
    $Module = request::$Module;
    $Page   = request::$Page;
  
    //load xml config of page
    $Xml_File = "modules/".$Module."/pages/views/".
    $Page.".page.view.xml";
    $Configs = xml::arr_from_file($Xml_File);
    $Skin = $Configs["skin"];

    //save the configs of page
    self::$Miscs["page"] = array();
    self::$Miscs["page"]["configs"] = $Configs;
    
    //set skin files
    $Css_File  = "skins/".$Skin."/".$Skin.".css";
    $Html_File = "skins/".$Skin."/".$Skin.".php";
    $Js_File   = "skins/".$Skin."/".$Skin.".js";
    
    //load skin html file
    require_once($Html_File);
    echo "\r\n";
    
    //load skin css file
    echo "<style>\r\n";
    echo "/*$Css_File*/\r\n";
    require_once($Css_File);
    echo "\r\n";
    echo "</style>\r\n";
    
    //load skin js file
    echo "<script>\r\n";
    echo "//$Js_File\r\n";
    require_once($Js_File);
    echo "\r\n";
    echo "</script>\r\n";
  }
  
  /**
   * Get module information from the xml file of module
   *
   * @return array The multi-level array containing info from module xml
   */
  public static function get_module_info() {
    $Module   = request::$Module;
    $Xml_File = "modules/$Module.xml";
    return xml::arr_from_file($Xml_File);
  }
  
  /**
   * Get website name in current language
   * 
   * @return string The website name stated in config xml
   */
  public static function get_website_name() {
    return phrase::__(self::$Configs["website"]);
  }
  
  /**
   * Get the page title defined in page xml
   * 
   * @return string The page title defined in page xml file
   */
  public static function get_page_title() {
    return phrase::__(self::$Miscs["page"]["configs"]["title"]);
  }
  
  /**
   * Get the layout name defined in page xml
   *
   * @return string The name of the layout
   */
  public static function get_layout_name() {
    return self::$Miscs["page"]["configs"]["layout"];
  }
  
  /**
   * Get the skin name defined in page xml
   *
   * @return string The name of the skin
   */
  public static function get_skin_name() {
    return self::$Miscs["page"]["configs"]["skin"];
  }
  
  /**
   * Get base url for all links
   *
   * @return string The base URL for all internal links
   */
  public static function get_base_url() {
    $Host_Name = $_SERVER["HTTP_HOST"]; 
    $Cur_Path  = $_SERVER["PHP_SELF"]; 
    $Path_Info = pathinfo($Cur_Path); 
    $Protocol  = strtolower(substr($_SERVER["SERVER_PROTOCOL"],0,5))
    =="https"? "https://":"http://";
 
    return $Protocol.$Host_Name.$Path_Info["dirname"]."/";
  }  
  
  /**
   * Get file extension 
   *
   * @param  string $File_Path The path to a file
   * @return string The file extension in lowercase
   */
  public static function get_file_ext($File_Path) {
    $Parts = pathinfo($File_Path);
    return $Parts["extension"];
  }
  
  /**
   * Render a block
   *
   * @param string $Block  The name of block to render
   * @param string $Module The module name in where the block code is
   */
  public static function render_block($Block,$Module=null) { 
    if ($Module==null)
      $Module = request::$Module;
    
    //[1] load block model
    $Block_Model_File = "modules/".$Module."/blocks/models/".
    $Block.".block.model.php";
    require_once($Block_Model_File);
    
    //create block model instance
    $Block_Model_Class = $Block."_block_model";
    $Block_Model = new $Block_Model_Class();
    
    //[2] load block controller
    $Block_Controller_File = "modules/".$Module."/blocks/controllers/".
    $Block.".block.controller.php";
    require_once($Block_Controller_File);
    
    //create block controller instance
    $Block_Controller_Class = $Block."_block_controller";
    $Block_Controller = new $Block_Controller_Class();
    
    //put the block model instance into block controller
    $Block_Controller->Type  = "block";
    $Block_Controller->Name  = $Block;
    $Block_Controller->Model = $Block_Model;
    
    //ACTUAL HTML RENDERING:
    $Block_Controller->Module = $Module;
    $Block_Controller->render_html();
  }
  
  /**
   * Render the web page in the layout specified in config file
   */
  public static function render_html() {
    $Module  = request::$Module;
    $Page    = request::$Page;
    $Actions = request::$Actions;
    $Params  = request::$Params;
    
    //[1] load page model
    $Page_Model_File = "modules/".$Module."/pages/models/".
    $Page.".page.model.php";
    require_once($Page_Model_File);
    
    //create page model instance
    $Page_Model_Class = $Page."_page_model";
    $Page_Model = new $Page_Model_Class();
    
    //[2] load page controller
    $Page_Controller_File = "modules/".$Module."/pages/controllers/".
    $Page.".page.controller.php";
    require_once($Page_Controller_File);
    
    //create page controller instance
    $Page_Controller_Class = $Page."_page_controller";
    $Page_Controller = new $Page_Controller_Class();    
    
    //put the page model instance into page controller
    $Page_Controller->Type  = "page";
    $Page_Controller->Name  = $Page;
    $Page_Controller->Model = $Page_Model;
    
    //[3] load layout model
    $Layout = self::$Miscs["page"]["configs"]["layout"];
    $Layout_Model_File = "modules/shared/layouts/models/".
    $Layout.".layout.model.php";
    require_once($Layout_Model_File);
    
    //create layout model instance
    $Layout_Model_Class = $Layout."_layout_model";
    $Layout_Model = new $Layout_Model_Class();
    
    //[4] load layout controller
    $Layout_Controller_File = "modules/shared/layouts/controllers/".
    $Layout.".layout.controller.php";
    require_once($Layout_Controller_File);
    
    //create layout controller instance
    $Layout_Controller_Class = $Layout."_layout_controller";
    $Layout_Controller = new $Layout_Controller_Class();
    
    //put the page controller instance into layout controller
    $Layout_Controller->Type = "layout";
    $Layout_Controller->Name = $Layout;
    $Layout_Controller->Page = $Page_Controller;
    
    //ACTUAL HTML RENDERING:
    $Layout_Controller->render_html();
  }
  
  /**
   * Finalise the whole framework
   */
  public static function finalise() {
    ob_end_flush();
  }
}

//END-OF-FILE